package actionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import util.DomainUtils;



public class AddContactGroupValidationForm extends ActionForm{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String group_name = null;
	private String[] contacts_of_group = null;
	
	public String getGroup_name() {
		return group_name;
	}
	public void setGroup_name(String group_name) {
		this.group_name = DomainUtils.trim(group_name);
	}
	public String[] getContacts_of_group() {
		return contacts_of_group;
	}
	public void setContacts_of_group(String[] contacts_of_group) {
		this.contacts_of_group = contacts_of_group;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.group_name  = null;
		this.contacts_of_group = null;
	}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession(false);


		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"session.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}


		//Checking group name
		if(group_name == null || group_name.isEmpty())
		{
			errors.add("group_name", new ActionMessage(
					"addcontactgroup.group_name.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		if(DomainUtils.startByALetter(group_name) == false && 
				DomainUtils.startByUnderscore(group_name) == false) {

			errors.add("group_name", new ActionMessage(
					"addcontactgroup.group_name.startbyletterorunderscore.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		if(DomainUtils.containsALetter(group_name) == false) {
			errors.add("group_name", new ActionMessage(
					"addcontactgroup.group_name.containoneletter.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		if(DomainUtils.match(group_name, "\\w+") == false){
			
			errors.add("group_name", new ActionMessage(
					"addcontactgroup.group_name.containnostrangecharacter.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}
		
		return errors;
	}

}
